#include "mycommon.h"
#include "oled_GUI.h"
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <signal.h>

extern const unsigned char logo[][8];
const char* net_type[] = {
    "--",
    "2G",
    "3G",
    "4G"
};
char *status[] = {
    "module powerup...",
    "read simcard ok...",
    "detect csq[23]...",
    "detect sim reg[1]..."
};

static struct itimerval oldtv;
void set_timer()
{
    struct itimerval itv;
    itv.it_interval.tv_sec = 0;
    itv.it_interval.tv_usec = 500000;
    itv.it_value.tv_sec = 0;
    itv.it_value.tv_usec = 500000;
    setitimer(ITIMER_REAL, &itv, &oldtv);
}

void signal_handler(int m)
{
    OLED_Screen_display();//缓存屏幕填充
    OLED_CLS_SF();//最好加一个缓存清屏函数
}



int main(void)
{
    int i = 0, j = 0;
    time_t timer;//time_t就是long int 类型
    struct tm *tblock;
    char disbuff[100], dat = ' ';

    OLED_Init(); //初始化OLED
    OLED_CLS_SF();//最好加一个缓存清屏函数
    srand(time(NULL));
    signal(SIGALRM, signal_handler);
    set_timer();

    while (1)
    {
        timer = time(NULL);
        tblock = localtime(&timer);
        //snprintf(disbuff, 20 , asctime(tblock));
        if(j%2){
            sprintf(disbuff,"%02d/%02d %02d:%02d", tblock->tm_mon+1, tblock->tm_mday, tblock->tm_hour, tblock->tm_min);
        }else{
            sprintf(disbuff,"%02d/%02d %02d %02d", tblock->tm_mon+1, tblock->tm_mday, tblock->tm_hour, tblock->tm_min);
        }
        OLED_ShowStr_SF(0, 0, (unsigned char *)net_type[i], 1);
        OLED_DrawBMP_SF(16, 0, 24, 1, logo[i]);
        OLED_ShowStr_SF((128 - strlen(disbuff)*6)/2, 0, (uint8_t *)disbuff, 1);
        OLED_DrawBMP_SF(119, 0, 127, 1, logo[6 + (j%2)]);

        sprintf(disbuff, status[i]);
        OLED_ShowStr_SF((128 - strlen(disbuff)*6)/2, 8, (uint8_t *)disbuff, 1);

        sprintf(disbuff, "E: T[%d] R[%d]", rand()%10000, rand()%10000);
        OLED_ShowStr_SF((128 - strlen(disbuff)*6)/2, 16, (uint8_t *)disbuff, 1);

        sprintf(disbuff, "Hex[%02x] ASCII[%c]", dat, dat);
        if(++dat >= '~'){
            dat = ' ';
        }
        OLED_ShowStr_SF((128 - strlen(disbuff)*6)/2, 24, (uint8_t *)disbuff, 1);
        usleep(500000);
        if(++j > 10){
            j = 0;
            if(++i > 3){
                i = 0;
            }
        }
    }
    exit(EXIT_SUCCESS);
}
